package cn.bdqn.asecu.service;

import cn.bdqn.asecu.utility.Utility_departmetn;
import cn.bdqn.pojo.Department;
import cn.bdqn.pojo.Maxdepartment;
import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.User;
import cn.bdqn.repository.Asecu_DepartmentRepository;
import cn.bdqn.repository.Asecu_UserRepository;
import cn.bdqn.repository.Asecu_jurisdictionRoepository;
import cn.bdqn.repository.Asecu_superiordepartment;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class Auecu_lineService {

    @Resource
    private Asecu_UserRepository asecu_userRepository;
    @Resource
    private Asecu_jurisdictionRoepository asecu_jurisdictionRoepository;
    @Resource
    private Asecu_DepartmentRepository asecu_departmentRepository;
    @Resource
    private Asecu_superiordepartment asecu_superiordepartment;

    //上级部门
    public Maxdepartment SelMdID(Integer id){
        return asecu_superiordepartment.findById(id).get();
    }
    //查询总部门
    public List<Maxdepartment>  Selzgeneral(){
        List<Maxdepartment> all = asecu_superiordepartment.findAll();
        return all;
    }
    //树形
    public List<Utility_departmetn>  SeldepartmentShow(){
        List<Utility_departmetn> utility_departmetn=new ArrayList<>();
        List<Department> all = asecu_departmentRepository.findAll();
        for (Department d: all) {
            Utility_departmetn u=new Utility_departmetn();
            Maxdepartment m=asecu_superiordepartment.findById(d.getMaxd_id()).get();
            u.setDepartment(d);
            u.setMaxdepartment(m);
            utility_departmetn.add(u);
        }
        return utility_departmetn;
    }
    //权限id查询
    public Role Permissionquery(Integer id){
        Optional<Role> byId = asecu_jurisdictionRoepository.findById(id);
        return byId.get();
    }

    //部门id查询
    public Department departmentSel(Integer id){
        Department department = asecu_departmentRepository.findById(id).get();
        return department;
    }
     //上级查询部门
    public  List<Department> seldata(Integer id){
         Specification<Department> spec=new Specification<Department>() {
             @Override
             public Predicate toPredicate(Root<Department> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cd) {
                 return cd.or(cd.equal(root.get("maxd_id"),id));
             }
         };
        return asecu_departmentRepository.findAll(spec);
    }

    //查询所有员工
    public List<User>  SelAll(Integer id,Integer pdid){
        if (id!=0){
           if (id==-1){
               return asecu_userRepository.findAll();
           }
           if (id>0){
               Specification<User> spec=new Specification<User>() {
                   @Override
                   public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cd) {
                    List<Predicate> list =new ArrayList<>();
                    List<Department> seldata = seldata(id);

                    for (Department d: seldata) {
                        list.add(cd.equal(root.get("departmentId"),d.getId()));
                    }

                       Predicate[] arr=new Predicate[list.size()];
                       return cd.or(list.toArray(arr));
                   }
               };
               return asecu_userRepository.findAll(spec);
           }

        }

        if (pdid!=0){
            Specification<User> spec=new Specification<User>() {
                @Override
                public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cd) {
                    return cd.or(cd.equal(root.get("departmentId"),pdid));
                }
            };
            return asecu_userRepository.findAll(spec);
        }

        return asecu_userRepository.findAll();
    }

    //部门查询全部
    public List<Department> departmentSelAll(){
        return asecu_departmentRepository.findAll();
    }

    //用户添加
    public User  insetUser(User user){
        User save = asecu_userRepository.save(user);
        return save;
    }

    //id查询用户
    public User SelUser(Integer id){
        User user = asecu_userRepository.findById(id).get();
        return user;
    }

    //删除用户
    public void delUser(Integer id){
        asecu_userRepository.deleteById(id);
    }

    //修改密码
    public int  uplUserpasswrd(User user){
        int i = asecu_userRepository.updatPwd(user.getPassword(), user.getId());
        return i;
    }

    //修改状态
    public int upluserstate(int id,String state){
        return asecu_userRepository.updatstate(id,state);
    }

    //编辑
    public int uplportion(User user){
        return asecu_userRepository.updatportion(user.getId(),user.getName(),user.getXname(),user.getJobs(),user.getQualification());
    }

    //显示roel
    public List<Role> SelRoelAll(){
        return asecu_jurisdictionRoepository.findAll();
    }

    //修改权限
    public int uplRoot(User user){
        return asecu_userRepository.updatroel(user.getId(), user.getRoleId());
    }
}
